package com.sharedlease.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharedlease.entity.Record;
import com.sharedlease.entity.R;
import com.sharedlease.service.RecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-03-22
 */
@RestController
@Api(tags = "用户浏览产品记录接口")
@RequestMapping("/record")
public class RecordController {
    @Autowired
    public RecordService recordService;

    @GetMapping("/getById/{id}")
    @ApiOperation("根据一个id查询浏览记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的浏览记录id", defaultValue = "1"),

    }
    )
    public Record getById(@PathVariable Long id){
        Record record = recordService.getById(id);
        return record;
    }

    @GetMapping("/getByUid/{uid}")
    @ApiOperation("根据用户id查询浏览记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "要查询的浏览记录的uid", defaultValue = "1"),
    }
    )
    public List<Record> getByUid(@PathVariable Long uid){
        QueryWrapper<Record> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid+"");
        List<Record> list = recordService.list(wrapper);
        return list;
    }

    @GetMapping("/list")
    @ApiOperation("查询全部浏览记录")
    public List<Record> list(){
        List<Record> list = recordService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一条浏览记录")
    public R save(@RequestBody Record record) {

        return R.operate(recordService.save(record));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改一条浏览记录")
    R update(@RequestBody Record record){
        if(recordService.updateById(record)){
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除一条浏览记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的浏览记录id", defaultValue = "-1"),

    }
    )
    R removeById(@PathVariable Long id) {

        return R.operate (recordService.removeById(id));
    }
    

}

